/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package studentinforedux;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.bind.JAXBException;

/**
 *
 * @author theInterloper
 */
public class Bits {
    
    public static StudentDB setData (StudentDB studentdb, Student student) {
        
        ArrayList<Student> studentList = new ArrayList<Student>();
        studentList.add(student);
        studentdb.setSTUList(studentList);
        
        return studentdb;
    }

    static void printStudent(StudentDB studentdb) {
        System.out.println("The students in the database are:\n");
            for(int i = 0; i< studentdb.getSTULists().toArray().length; i++){
                 System.out.println("Student " + (i + 1) + ":\n"
                + "Name:          " +studentdb.getSTULists().get(i).getNAMEs() + "\n" 
                + "Date Of Birth: " +studentdb.getSTULists().get(i).getDOB() + "\n"
                + "K Number:      " +studentdb.getSTULists().get(i).getKNUM() + "\n"
                + "Password:      " +studentdb.getSTULists().get(i).getPASSWORD() + "\n\n");
            }
    }
    
    public static void writeTest() throws JAXBException, IOException {
        StudentDB studentdb = new StudentDB();
        Student student = new Student();
        setTest(student);
        setData(studentdb, student);
        ReadWrite.write(studentdb);
    }
    
    public static Student setTest(Student student) {
            
        student.setNAME("James MacDonald");
        student.setKNUM("K00112173");
        student.setDOB("21/09/1981");
        student.setPASSWORD("password");
        
        return student;
    }
     public static void addStudent()throws JAXBException, IOException {
        
        StudentDB studentdb = new StudentDB();
        studentdb = ReadWrite.read(); //db contains students from xml
        
        ArrayList<Student> Data = new ArrayList<Student>();
        Data = studentdb.getSTULists();
        
        Student newStudent = new Student(); //new student created 
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Name: ");
        newStudent.setNAME(scan.nextLine());
        System.out.println("Enter date of birth: ");
        newStudent.setDOB(scan.nextLine());
        System.out.println("Enter K Number: ");
        newStudent.setKNUM(scan.nextLine());
        System.out.println("Enter Password: ");
        newStudent.setPASSWORD(scan.nextLine());
        
        Data.add(newStudent);
        studentdb.setSTUList(Data);
        
        ReadWrite.write(studentdb);
    }
     
    public static void deleteStudent() throws JAXBException, IOException {
        
        StudentDB studentdb = new StudentDB();
        studentdb = ReadWrite.read();
        
        printStudent(studentdb);
        
        ArrayList<Student> allData = new ArrayList<Student>();
        allData = studentdb.getSTULists();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter an index number to delete");
        int input = scan.nextInt();
        allData.remove(input-1);
        studentdb.setSTUList(allData);
        ReadWrite.write(studentdb);
        
    }


    static void sorter(int trigger) throws JAXBException, IOException {
        
        StudentDB studentdb = new StudentDB();
        studentdb = ReadWrite.read();
        
        Map<String,Student> stuMap = new HashMap<String,Student>();
        
        ArrayList<Student> studentlist = new ArrayList<Student>();
        studentlist = studentdb.getSTULists();
        
        if(trigger == 0){
            for(Student i:studentlist){
                 stuMap.put(i.getNAMEs(),i);
            }
        }
        else{
            for(Student i:studentlist){
                 stuMap.put(i.getKNUM(),i);
            }
        }
         
         Set<String> keys = stuMap.keySet();
         
         TreeSet<String> sortedKeys = new TreeSet<String>(keys);

         for(String i:sortedKeys){
             Student a = stuMap.get(i);
             a.print();
         }
    }

    static void find()throws JAXBException, IOException {
        
        StudentDB studentdb = new StudentDB();
        studentdb = ReadWrite.read();
     
        Scanner scan = new Scanner(System.in);
        System.out.println("\nEnter a K Number (00X)");
        int input = Integer.parseInt(scan.nextLine());
        
        for(int i = 0; i< studentdb.getSTULists().toArray().length; i++){
            
            int knum = Integer.parseInt(studentdb.getSTULists().get(i).getKNUM());
            if(knum == input){
               studentdb.getSTULists().get(i).print();
            } else{
                System.out.println("The student is not in the database");
            }   
           
        }
 
    }
    
}
